
/**
*┌──────────────────────────────────────────────────────────────┐
*│　描    述：用户入金汇率表                                                    
*│　作    者：gaotang                                            
*│　版    本：1.0    模板代码自动生成                                                
*│　创建时间：2019-02-20 15:07:13                             
*└──────────────────────────────────────────────────────────────┘
*┌──────────────────────────────────────────────────────────────┐
*│　命名空间： BestTrader.Cms.Services                                  
*│　类    名： UCExchangeRateManagerService                                    
*└──────────────────────────────────────────────────────────────┘
*/
using BestTrader.Cms.Core.Extensions;
using BestTrader.Cms.IRepository;
using BestTrader.Cms.IServices;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using BestTrader.Cms.ViewModels;
using BestTrader.Cms.Models;
using AutoMapper;
using Microsoft.Extensions.Logging;

namespace BestTrader.Cms.Services
{
    public class UCExchangeRateManagerService : IUCExchangeRateManagerService
    {
        private readonly IUCExchangeRateManagerRepository _repository;
        private readonly IUCExchangeRateRepository _ucExchangeRateRepository;
        private readonly IMapper _mapper;
        private readonly ILogger<UCExchangeRateManagerService> _logger;


        public UCExchangeRateManagerService(
            IUCExchangeRateManagerRepository repository, 
            IUCExchangeRateRepository ucExchangeRateRepository, 
            IMapper mapper,
            ILogger<UCExchangeRateManagerService> logger)
        {
            _repository = repository;
            _ucExchangeRateRepository = ucExchangeRateRepository;
            _logger = logger;
            _mapper = mapper;

        }

        /// <summary>
        /// 获取用户汇率设置
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public TableDataResult<UCExchangeRateManager> GetExchangeRateManagerByManagerId(string UserId)
        {
            var result = new TableDataResult<UCExchangeRateManager>();
            var managerId = UserId.ToInt32() ?? 0;
            string conditions = $" WHERE  ManagerId = {managerId}";
            var data = _repository.GetList(conditions).FirstOrDefault();
            result.Data = data;
            return result;
        }

        /// <summary>
        /// 添加用户汇率关系
        /// </summary>
        /// <param name="item"></param>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public BaseResult AddOrModifyExchangeRate(UCExchangeRateManagerModel item, String UserId)
        {

            var result = new BaseResult();
            var entity = _mapper.Map<UCExchangeRateManager>(item);

            //修改
            if (item.Id > 0)
            {
                var model = _repository.Get(item.Id);
                model.InCoin = entity.InCoin;
                model.OutCoin = entity.OutCoin;

                if (_repository.Update(model) > 0 )
                {
                    _logger.LogTrace($"充值中心用户出入金汇率关系[编辑] UserId：{UserId},ExChangeRateManagerId:{model.Id}");
                    result.ResultCode = ResultCodeAddMsgKeys.CommonObjectSuccessCode;
                    result.ResultMsg = ResultCodeAddMsgKeys.CommonObjectSuccessMsg;
                }
                else
                {
                    result.ResultCode = ResultCodeAddMsgKeys.CommonExceptionCode;
                    result.ResultMsg = ResultCodeAddMsgKeys.CommonExceptionMsg;
                }

            }
            else
            {
                //新增
                entity.AddManagerId = UserId.ToInt32();
                entity.AddTime = DateTime.Now;

                if (_repository.Insert(entity) > 0)
                {
                    _logger.LogTrace($"充值中心用户出入金汇率关系[新增] UserId：{UserId},ExChangeRateManagerId:{entity.Id}");
                    result.ResultCode = ResultCodeAddMsgKeys.CommonObjectSuccessCode;
                    result.ResultMsg = ResultCodeAddMsgKeys.CommonObjectSuccessMsg;
                }
                else
                {
                    result.ResultCode = ResultCodeAddMsgKeys.CommonExceptionCode;
                    result.ResultMsg = ResultCodeAddMsgKeys.CommonExceptionMsg;
                }

            }
            return result;


        }


        /// <summary>
        /// 获取用户出入金汇率
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public TableDataResult<UCExchangeRateInOutRateModel> GetInOutExchangeRateByManagerId(string UserId)
        {
            var result = new TableDataResult<UCExchangeRateInOutRateModel>();
            var resultModel = GetExchangeRateManagerByManagerId(UserId);
            var entity = resultModel.Data;
            var DefaultEntity = _ucExchangeRateRepository.GetDefaultExchangeRate();

            if (entity != null && DefaultEntity != null)
            {

                var InCoinEntity = _ucExchangeRateRepository.Get(entity.InCoin);
                var OutCoinEntity = _ucExchangeRateRepository.Get(entity.OutCoin);
                var InCoinExchangeRate = InCoinEntity == null ? DefaultEntity.ProjectPrice : InCoinEntity.ProjectPrice;
                var InCoinUnit = InCoinEntity == null ? DefaultEntity.ProjectName : InCoinEntity.ProjectName;
                var InCoinUnitCode = InCoinEntity == null ? DefaultEntity.ProjectCode : InCoinEntity.ProjectCode;

                var OutCoinExchangeRate = OutCoinEntity == null ? DefaultEntity.ProjectPrice : OutCoinEntity.ProjectPrice;
                var OutCoinUnit = OutCoinEntity == null ? DefaultEntity.ProjectName : OutCoinEntity.ProjectName;
                var OutCoinUnitCode = OutCoinEntity == null ? DefaultEntity.ProjectCode : OutCoinEntity.ProjectCode;

                result.Data = new UCExchangeRateInOutRateModel
                {
                    InCoinExchangeRate = InCoinExchangeRate,
                    InCoinUnit = InCoinUnit,
                    InCoinUnitCode = InCoinUnitCode,
                    OutCoinExchangeRate = OutCoinExchangeRate,
                    OutCoinUnit = OutCoinUnit,
                    OutCoinUnitCode = OutCoinUnitCode,
                };
            }
            else
            {
                result.Data = new UCExchangeRateInOutRateModel
                {
                    InCoinExchangeRate = DefaultEntity?.ProjectPrice,
                    InCoinUnit = DefaultEntity?.ProjectName,
                    InCoinUnitCode = DefaultEntity?.ProjectCode,
                    OutCoinExchangeRate = DefaultEntity?.ProjectPrice,
                    OutCoinUnit = DefaultEntity?.ProjectName,
                    OutCoinUnitCode = DefaultEntity?.ProjectCode,

                };

            }

            return result;
        }

    }
}